import os

import requests
from 运维工具.登录逻辑.utils.base64_to_image import decode_base64_to_image
def getCode(**kwargs):

    print(kwargs)
    cookies = {
        'JSESSIONID': kwargs.get('JSESSIONID'),
        'tempTokenKey': kwargs.get('tempTokenKey'),
        # 'tempTokenKey': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhbm9ueW1vdXMiLCJhdWQiOiIiLCJzZXJ2ZXIiOiJodHRwczovL2d6bWguendmdy5ndWl6aG91Lmdvdi5jbi9sb2dpbi8iLCJyb2xlIjoiIiwic3RhdHVzIjoiMyIsImlzcyI6Imluc3B1ciIsImp0aSI6IjYxN2U1YzExLTI1NTUtNDhjNC04YTRiLTEyOWU3OGU2ZjczYiIsImlhdCI6MTc1Nzg0MzM2OCwiZXhwIjoxNzU3ODczMzY4fQ.H5IinYS8asK2ZaBn6FrIZV9d1mHNpltYbFEsknyexnFTc6sO2mVJrDiCAEOCR4TqaB5cQa0sUHZ4VTTg-JW8_w',
    }

    headers = {
        'Host': 'gzmh.zwfw.guizhou.gov.cn',
        'sec-ch-ua-platform': '"Windows"',
        'X-Requested-With': 'XMLHttpRequest',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'sec-ch-ua': '"Microsoft Edge";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'sec-ch-ua-mobile': '?0',
        'Origin': 'https://gzmh.zwfw.guizhou.gov.cn',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://gzmh.zwfw.guizhou.gov.cn/ucenter/simpleLogin?=&response_type=code&client_id=dgov-portal&redirect_uri=https://gzmh.zwfw.guizhou.gov.cn/login/callback.html&version=guizhou&isHLW=true',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    }

    params = {
        'isHLW': 'true',
    }

    data = {
        'csrf': kwargs.get('csrf'),
        'client_id': 'dgov-portal',
        'redirect_uri': 'https://gzmh.zwfw.guizhou.gov.cn/login/callback.html',
        'response_type': 'code',
        'scope': 'read',
        'state': kwargs.get('state'),
        'busi_uri': '',
        'is_weak_pwd': '0',
        'username': kwargs.get('username'),
        'password': kwargs.get('password'),
        'captcha': kwargs.get('captcha'),
    }

    response = requests.post(
        'https://gzmh.zwfw.guizhou.gov.cn/ucenter/web/guizhou/getCode',
        params=params,
        cookies=cookies,
        headers=headers,
        data=data,
    )
    # print(response)
    UnKey=response.json()['UnKey']

    pic=response.json()['pic']

    # 获取当前文件的目录
    current_dir = os.path.dirname(os.path.abspath(__file__))

    # 获取爷爷目录
    parent_dir = os.path.dirname(current_dir)  ##D:\pyWorkSpace\tkinter\运维工具


    # 构建到static/img的路径
    img_dir = os.path.join(parent_dir, 'static', 'img')

    decode_base64_to_image(pic, f'{img_dir}\人脸识别二维码.png')




    return {"UnKey":UnKey}
